package implementation.sort.QuickSort;

import anaydis.sort.SorterType;
import anaydis.sort.gui.SorterListener;
import com.sun.istack.internal.NotNull;
import implementation.sort.InsertionSort;

import java.util.Comparator;
import java.util.List;


public class QuickSortCutOff extends QuickSorter {
    private int m;
    private InsertionSort is;

    public QuickSortCutOff() {
        super(SorterType.QUICK_CUT);
        is = new InsertionSort();
        for (int j = 0; j < sorterListeners.size(); j++) {
            is.addSorterListener((SorterListener) sorterListeners.get(j));
        }
        m=10;
    }

    public void setM(int m) {
        this.m = m;
    }

    public <T> void sort(@NotNull Comparator<T> tComparator, @NotNull List<T> ts) {
        sort(tComparator, ts, 0, ts.size() - 1);
    }

    public <T> void sort(Comparator<T> comparator, List<T> list, int start, int end) {
        if (end <= start) return;

        if ((end - start) > m) {
            int i = partition(comparator, list, start, end);
            this.sort(comparator, list, start, i - 1);
            this.sort(comparator, list, i, end);

        } else {
           is.sort(comparator, list.subList(start, end+1));
        }
    }


}
